package com.micro.global;

import com.micro.download.MyThreadFactory;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.*;

/**
 * @author dufen
 */
public class MainTaskTreadPool extends ThreadPoolExecutor {
    private static int coreSize = ConfigInfo.getConfigInfo().getTaskCoreSize() == 0 ? 5 : ConfigInfo.getConfigInfo().getTaskCoreSize();
    private static MainTaskTreadPool INSTANCE = new MainTaskTreadPool(5, 5, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new MyThreadFactory("task-thread-group", "task-thread-name"));

    private MainTaskTreadPool() {
        super(coreSize, coreSize, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new MyThreadFactory("task-thread-group", "task-thread-name"));
    }

    private MainTaskTreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, @NotNull TimeUnit unit, @NotNull BlockingQueue<Runnable> workQueue, @NotNull ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public static MainTaskTreadPool getINSTANCE() {
        //如果本实例被关闭或初始化失败，则重新实例化
        if(INSTANCE==null || INSTANCE.isShutdown() || INSTANCE.isTerminated() || INSTANCE.isTerminating()){
            INSTANCE = new MainTaskTreadPool(5, 5, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new MyThreadFactory("task-thread-group", "task-thread-name"));
        }
        return INSTANCE;
    }
}
